package com.feinno.urcs.group.pd.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wuhao on 15-7-9.
 */
public class Content{
    private Map<String,String> headers;
    protected List<Content> contentList;
    private String contentType;
    private byte[] content;

    public Content(){
        headers = new HashMap<String, String>();
    }

    public Map<String, String> getHeaders() {
        return headers;
    }

    public void setHeaders(Map<String, String> headers) {
        this.headers = headers;
    }

    public List<Content> getContentList() {
        return contentList;
    }

    public void setContentList(List<Content> contentList) {
        this.contentList = contentList;
    }

    public String getContentType() {
        return contentType;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    public byte[] getContent() {
        return content;
    }

    public void setContent(byte[] content) {
        this.content = content;
    }

    /**
     * addHeader
     *
     * @param header
     */
    public void addHeader(String header){
        String []headerSpilt = header.split(":");
        if(headerSpilt.length >= 2){
            headers.put(headerSpilt[0], headerSpilt[1]);
        }
    }

    /**
     * addHeader
     *
     * @param headerName
     * @param headerValue
     */
    public void addHeader(String headerName, String headerValue){
        headers.put(headerName, headerName);
    }

    /**
     * getHeader
     *
     * @param headerName
     * @return
     */
    public String getHeader(String headerName){
        return headers.get(headerName);
    }

    /**
     * getContentByContentType
     *
     * @param contentType
     * @return
     */
    public Content getContentByContentType(String contentType){
        Content contentRtn = null;
        String contentTypeStr;
        for (Content content: contentList){
            contentTypeStr = content.getHeader("Content-Type");
            if(contentType != null && contentTypeStr.toLowerCase().indexOf(contentType) >= 0){
                contentRtn = content;
            }
        }
        return contentRtn;
    }
}
